package com.config;

import javax.sql.DataSource;
import com.config.datasource.DynamicDataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;


/**
 * @author HuangSuip
 */
@Configuration
@ConfigurationProperties(prefix = "datasource")
public class DataSourceConfig {

    private DataConfig dataConfig;

    @Primary
    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        System.out.println(dataConfig.getDriver());
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(dataConfig.getUrl());
        config.setUsername(dataConfig.getUsername());
        config.setPassword(dataConfig.getPassword());
        config.setDriverClassName(dataConfig.getDriver());
        return new HikariDataSource(config);
    }

    @Lazy
    @Bean(name = "dataSource2")
    public DataSource getDataSource2() {
        System.out.println(dataConfig.getDriver());
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://10.0.3.119:3306/local2?useUnicode=true&useSSL=false&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull");
        config.setUsername(dataConfig.getUsername());
        config.setPassword(dataConfig.getPassword());
        config.setDriverClassName(dataConfig.getDriver());
        return new HikariDataSource(config);
    }

    @Lazy
    @Bean(name = "readAndWrite")
    public DataSource getDataSourceAll(
        @Qualifier("dataSource2") DataSource dataSource2,
        @Qualifier("dataSource") DataSource dataSource
    ) {
        return new DynamicDataSource( dataSource, dataSource2);
    }

    public DataConfig getDataConfig() {
        return dataConfig;
    }

    public void setDataConfig(DataConfig dataConfig) {
        this.dataConfig = dataConfig;
    }

    public static class DataConfig {
        private String driver;
        private String url;
        private String username;
        private String password;

        public String getDriver() {
            return driver;
        }

        public void setDriver(String driver) {
            this.driver = driver;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}
